﻿using System;
using ClassLib.Classes.Common;
using DevExpress.XtraSplashScreen;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Sys;
using Sumen_Stock.LookAndFeel.Sumen_System;

namespace Sumen_Stock.LookAndFeel.Admin
{
    public partial class Frm_User : DevExpress.XtraEditors.XtraForm
    {


        #region variable

        Frm_User_Edit _frmUserEdit;
        private UserPresenter _userPresenter;
        private FromCode _fromCode = FromCode.FrmUser;

        #endregion
       

        /// <summary>
        /// //////////////////////////////////////////////////////////////////////////////////////
        /// </summary>

        #region Form

        public Frm_User()
        {
            InitializeComponent();
            _userPresenter = new UserPresenter();           
        }

        private void Frm_User_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        public override void Refresh()
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            base.Refresh();
            LoadForm();
            SplashScreenManager.CloseForm();
        }


        #endregion


        /// <summary>
        /// //////////////////////////////////////////////////////////////////////////////////////
        /// </summary>


        #region button event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            if (_frmUserEdit == null)
            {
                _frmUserEdit = new Frm_User_Edit();
                _frmUserEdit.EnumFormStatus = EnumFormStatus.Add;
                _frmUserEdit.ShowDialog();
            }
            else
            {
                _frmUserEdit.EnumFormStatus = EnumFormStatus.Add;
                _frmUserEdit.ShowDialog();
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonsArray1_btnEventDelete_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                if (ClsMsgBox.XacNhanXoaThongTin())
                {
                    if (_userPresenter.Delete(sUsername) > 0)
                    {
                        ClsMsgBox.ThongTin("Xóa nhân viên thành công,");
                        if (ClsPublic.Writelog == "1")
                        {
                            SysLogPresenter log = new SysLogPresenter();
                            log.Add(_fromCode, EnumFormStatus.Delete, ClsPublic.User.User_Username, "");
                        }
                    }
                }
            }
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                user users = _userPresenter.GetById(sUsername);
                if (users != null)
                {
                    if (_frmUserEdit == null)
                    {
                        _frmUserEdit = new Frm_User_Edit();
                        _frmUserEdit.User = users;
                        _frmUserEdit.EnumFormStatus = EnumFormStatus.Modify;
                        _frmUserEdit.ShowDialog();
                    }
                    else
                    {
                        _frmUserEdit.User = users;
                        _frmUserEdit.EnumFormStatus = EnumFormStatus.Modify;
                        _frmUserEdit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy nhân viên.");
                }
            }
        }

        #endregion


        /// <summary>
        /// //////////////////////////////////////////////////////////////////////////////////////
        /// </summary>


        #region grid event

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                user users = _userPresenter.GetById(sUsername);
                if (users != null)
                {
                    if (_frmUserEdit == null)
                    {
                        _frmUserEdit = new Frm_User_Edit();
                        _frmUserEdit.User = users;
                        _frmUserEdit.EnumFormStatus = EnumFormStatus.Modify;
                        _frmUserEdit.ShowDialog();
                    }
                    else
                    {
                        _frmUserEdit.User = users;
                        _frmUserEdit.EnumFormStatus = EnumFormStatus.Modify;
                        _frmUserEdit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy nhân viên.");
                }
            }
        }

        #endregion


        /// <summary>
        /// //////////////////////////////////////////////////////////////////////////////////////
        /// </summary>


        #region Method


        private void LoadForm()
        {
            gridControl1.DataSource = _userPresenter.GetAll();
        }

        #endregion

       

        

        
    }
}